﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SuperBall3D
{
    public class Pointer : StaticSuperBallObject
    {
        public Vector3 point;
        SpriteFont font;
        SpriteBatch spriteBatch;
        public Vector3 pointNorm;
        public Pointer(Game g, Vector3 pnt)
            : this(g, pnt, 1f, 1f, 1f)
        {
        }

        public Pointer(Game g, Vector3 pnt, float width, float height, float depth)
            : base(g, false, 1f, 1f)
        {
            point = pnt;
            Position = new Vector3(0,0,0);
            XScale = width;
            YScale = height;
            ZScale = depth;
        }


        public override void Initialize()
        {
            base.Initialize();
        }

        public void UpdatePointer(Vector3 playerPosition, float facingDirection)
        {
            point = SuperBallObject.platformList.ElementAt(0).Position;
            Position = playerPosition + new Vector3(0, 400, 0);
            Vector3 facingVector = Camera.Target;
            Vector3 PosNorm = Vector3.Normalize(new Vector3(Position.X, 0, Position.Z));
            Vector3 pointMinusPos = point - Position;
            pointNorm = Vector3.Normalize(new Vector3(pointMinusPos.X, 0, pointMinusPos.Z));
            RotationMatrix.Forward = Vector3.Normalize(point - Position);
            RotationMatrix.Right = Vector3.Normalize(Vector3.Cross(RotationMatrix.Forward, Vector3.Up));
            RotationMatrix.Up = Vector3.Normalize(Vector3.Cross(RotationMatrix.Right, RotationMatrix.Forward));
            RotationMatrix *= Matrix.CreateRotationY(MathHelper.PiOver2);
       }

        protected override void LoadContent()
        {
            superBallModel = Game.Content.Load<Model>("Models\\arrow");
            font = Game.Content.Load<SpriteFont>("posFont");
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            Geo = new Geometry(Geometry.GeometryType.Box, superBallModel, new Vector3(1000,1000,1000), 0, 0, 0);
            base.LoadContent();
        }

        public override string GenerateConstructor()
        {
            return "";
        }

        public override void Draw(GameTime gameTime)
        {
            
            /*
            spriteBatch.Begin( SpriteSortMode.Deferred, BlendState.AlphaBlend);
            int x = (int)Position.X;
            int y = 0; //(int)Position.Y;
            int z = (int)Position.Z;
            spriteBatch.DrawString(font, "Pos: " + x + " " + y + " " + z, new Vector2(15, 15), Color.White);
            float x2 = (float)pointNorm.X;
            float y2 = (float)pointNorm.Y;
            float z2 = (float)pointNorm.Z;
            spriteBatch.DrawString(font, "Norm: " + Math.Round(x2, 3) + " " + Math.Round(y2, 3) + " " + Math.Round(z2, 3), new Vector2(15, 35), Color.White);
            spriteBatch.DrawString(font, "Angle: " + angle, new Vector2(15, 55), Color.White);
           spriteBatch.End();
           DepthStencilState depthState;
           depthState = new DepthStencilState();
           depthState.DepthBufferEnable = true; 
            depthState.DepthBufferWriteEnable = true; 

           GraphicsDevice.DepthStencilState = depthState;
            */

            base.Draw(gameTime);
        }
    }
}
